package dcl.software.filesync.server.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.web.servlet.DispatcherServlet;

import com.mongodb.MongoClient;

@Configuration
@DependsOn("propertyPlaceholderConfigurer")
public class FileSyncServerBeanConfig {
	@Value("${mongo.database.ip}")
	private String databaseIP;
	@Value("${mongo.database.port}")
	private String databasePort;

	@Bean
	public MongoClient mongoClient() {
		MongoClient mongoClient = new MongoClient(databaseIP + ":" + databasePort);
		return mongoClient;
	}

	/*
	 * This was itended to handle returning exceptions, but didnt work as I
	 * anticipated. Commenting for now.
	 */
	@Bean
	public DispatcherServlet dispatcherServlet() {
		DispatcherServlet ds = new DispatcherServlet();
		ds.setThrowExceptionIfNoHandlerFound(true);
		return ds;
	}

	/*
	 * HTTPS handling - Uncomment for HTTPS connections
	 * 
	 * @Bean public EmbeddedServletContainerFactory servletContainer() {
	 * 
	 * // keytool -genkey -alias tomcat -storetype PKCS12 -keyalg RSA -keysize
	 * 2048 -keystore keystore.p12 -validity 3650 // keytool -list -v -keystore
	 * keystore.p12 -storetype pkcs12
	 * 
	 * // curl -u user:password -k https://127.0.0.1:9000/greeting ClassLoader
	 * classLoader = getClass().getClassLoader();
	 * classLoader.getResource("keystore.p12").getFile(); final String
	 * keystoreFile = classLoader.getResource("keystore.p12").getFile(); final
	 * String keystorePass = "StChristopher"; final String keystoreType =
	 * "PKCS12"; final String keystoreProvider = "SunJSSE"; final String
	 * keystoreAlias = "tomcatt";
	 * 
	 * TomcatEmbeddedServletContainerFactory factory = new
	 * TomcatEmbeddedServletContainerFactory();
	 * factory.addConnectorCustomizers(new TomcatConnectorCustomizer() {
	 * 
	 * @Override public void customize(Connector con) { con.setScheme("https");
	 * con.setSecure(true); Http11NioProtocol proto = (Http11NioProtocol)
	 * con.getProtocolHandler(); proto.setSSLEnabled(true);
	 * proto.setKeystoreFile(keystoreFile); proto.setKeystorePass(keystorePass);
	 * proto.setKeystoreType(keystoreType);
	 * proto.setProperty("keystoreProvider", keystoreProvider);
	 * proto.setKeyAlias(keystoreAlias); } });
	 * 
	 * return factory; }
	 */

	/*
	 * @Bean
	 * 
	 * @Inject public EmbeddedServletContainerCustomizer
	 * containerCustomizer(@Value("${keystore.file}") final String keystoreFile,
	 * 
	 * @Value("${keystore.password}") final String keystorePassword,
	 * 
	 * @Value("${keystore.type}") final String keystoreType,
	 * 
	 * @Value("${keystore.alias}") final String keystoreAlias) throws
	 * FileNotFoundException { final String absoluteKeystoreFile =
	 * ResourceUtils.getFile(keystoreFile).getAbsolutePath();
	 * 
	 * return (ConfigurableEmbeddedServletContainerFactory factory) -> {
	 * TomcatEmbeddedServletContainerFactory containerFactory =
	 * (TomcatEmbeddedServletContainerFactory) factory;
	 * containerFactory.addConnectorCustomizers(new TomcatConnectorCustomizer()
	 * {
	 * 
	 * @Override public void customize(Connector con) { con.setScheme("https");
	 * con.setSecure(true); Http11NioProtocol proto = (Http11NioProtocol)
	 * con.getProtocolHandler(); proto.setSSLEnabled(true);
	 * proto.setKeystoreFile(absoluteKeystoreFile);
	 * proto.setKeystorePass(keystorePassword);
	 * proto.setKeystoreType(keystoreType);
	 * proto.setProperty("keystoreProvider", "SunJSSE");
	 * proto.setKeyAlias(keystoreAlias); } }); }; }
	 */
}
